/*
 * A Gneral purpose java based Math Library created by Susanta Tewari.
 * Copyright is protected. Contact Susanta Tewari at <statsusant@yahoo.com>
 */

package org.math.series;

import org.math.calculas.IntegerFunction;
import org.math.calculas.IntegerVariable;
import org.math.calculas.ScalarIntegerVariable;
import org.math.calculas.ScalarVariable;
import org.math.calculas.Variable;
import org.math.data.DataValue;
import org.math.data.ScalarDataValue;
import org.math.data.storage.DoubleStorage;
import org.math.data.storage.StorageType;
import org.math.factory.DataValueFactory;

/**
 * Used in Geometric Test. Models a geometric function
 * f(x,n) =  x^n; n >= 1 and x is Real. This is an example of <code>IntegerFunction</code>
 * @author Susanta Tewari <stewari@yahoo.com>
 */
public class GeometricFunction implements IntegerFunction {

    /** Field description */
    private ScalarVariable varX = new ScalarVariable("x");

    /** Field description */
    private ScalarIntegerVariable varN = new ScalarIntegerVariable("n");

    public GeometricFunction(double x) {
        varX.setValue(new ScalarDataValue(new DoubleStorage(x)));
    }

    public IntegerVariable getIntegerVariable() {
        return this.varN;
    }

    public DataValue eval() {


        // alternative way:
        double result = Math.pow(varX.getValue().getValue(), varN.getValue().getValue() - 1);

        return DataValueFactory.createScalarDataValue(StorageType.DOUBLE, result);


        // return varX.getValue().pow((int) varN.getValue().getValue() - 1);
    }

    public Variable[] getVariables() {
        return new Variable[] { varX, varN };
    }
}
